package com.vichat.user.service.impl;

import com.vichat.common.util.NumberUtils;
import com.vichat.common.vo.PageFilter;
import com.vichat.core.dao.BaseDaoI;
import com.vichat.user.entity.SysStaticData;
import com.vichat.user.service.IStaticDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by user on 2016/11/17.
 */
@Service("staticDataService")
public class StaticDataServiceImpl implements IStaticDataService {

    @Autowired
    private BaseDaoI<SysStaticData> staticDataDao;

    /**
     * 拼装排序字段
     *
     * @param pf
     * @return
     */
    private String orderHql(PageFilter pf) {
        String orderString = "";
        if (pf != null) {
            if ((pf.getSort() != null) && (pf.getOrder() != null)) {
                orderString = " order by " + pf.getSort() + " " + pf.getOrder();
            }
        }
        return orderString;
    }

    public List<SysStaticData> queryStaticDatas() {
        String hql = "from SysStaticData where 1 = 1";
        List<SysStaticData> sysStaticDatas = staticDataDao.find(hql);
        return sysStaticDatas;
    }

    public SysStaticData getStaticData(String codeKey, String codeValue) {
        String hql = "from SysStaticData where 1 = 1 and codeKey = :codeKey and codeValue = :codeValue";
        HashMap params = new HashMap();
        params.put("codeKey", codeKey);
        params.put("codeValue", codeValue);
        return staticDataDao.get(hql, params);
    }

    /**
     * 获取对应的数据
     *
     * @param codeKey
     * @return
     */
    @Override
    public List<SysStaticData> getStaticData(String codeKey) {
        String hql = "from SysStaticData where 1 = 1 and codeKey = :codeKey ";
        HashMap params = new HashMap();
        params.put("codeKey", codeKey);
        return staticDataDao.find(hql, params);
    }

    /**
     * 保存或修改系统参数
     *
     * @param sysStaticData
     * @throws Exception
     */
    public void saveOrUpdStaticData(SysStaticData sysStaticData) throws Exception {
        String exitsStaticData = countExitsStaticData(sysStaticData);
        if (StringUtils.isNotBlank(exitsStaticData)) {
            throw new Exception(exitsStaticData);
        }

        if (!NumberUtils.isNotNullOrZero(sysStaticData.getCodeId())) {
            staticDataDao.save(sysStaticData);
        } else {
            SysStaticData sysStaticDataFDB = queryStaticDataByCodeId(sysStaticData.getCodeId());
            sysStaticDataFDB.setCodeId(sysStaticData.getCodeId());
            sysStaticDataFDB.setCodeKey(sysStaticData.getCodeKey());
            sysStaticDataFDB.setCodeValue(sysStaticData.getCodeValue());
            sysStaticDataFDB.setCodeName(sysStaticData.getCodeName());
            sysStaticDataFDB.setSeqBy(sysStaticData.getSeqBy());
            staticDataDao.update(sysStaticDataFDB);
        }
    }

    /**
     * 按条件验证系统参数
     *
     * @param queryBean
     * @return
     * @throws Exception
     */
    public String countExitsStaticData(SysStaticData queryBean) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        String hql = "select count(*) from SysStaticData where 1 = 1";
        if (NumberUtils.isNotNullOrZero(queryBean.getCodeId())) {
            hql += " and codeId <> :codeId";
            params.put("codeId", queryBean.getCodeId());
        }
        if (StringUtils.isNotBlank(queryBean.getCodeKey())) {
            hql += " and codeKey = :codeKey";
            params.put("codeKey", queryBean.getCodeKey());
        }
        if (StringUtils.isNotBlank(queryBean.getCodeValue())) {
            hql += " and codeValue = :codeValue";
            params.put("codeValue", queryBean.getCodeValue());
        }
        long sysExitsStaticDataCount = staticDataDao.count(hql, params);
        if (sysExitsStaticDataCount > 0) {
            return "枚举VALUE不能重复，请重新录入!";
        }

        if (StringUtils.isNotBlank(queryBean.getCodeName())) {
            hql += " and codeName = :codeName";
            params.put("codeName", queryBean.getCodeName());
        }
        sysExitsStaticDataCount = staticDataDao.count(hql, params);
        if (sysExitsStaticDataCount > 0) {
            return "枚举DESC不能重复，请重新录入!";
        }
        return "";
    }

    /**
     * 获取系统参数数量
     *
     * @param queryBean
     * @return
     * @throws Exception
     */
    public Long countSysStaticData(SysStaticData queryBean) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        String hql = "select count(*) from SysStaticData where 1 = 1";
        return staticDataDao.count(hql + sysStaticDataWhereHql(queryBean, params), params);
    }

    /**
     * 删除系统参数
     *
     * @param codeId
     * @throws Exception
     */
    public void delStaticData(long codeId) throws Exception {
        SysStaticData sysStaticDataFDB = staticDataDao.get(SysStaticData.class, codeId);
        staticDataDao.deleted(sysStaticDataFDB);
    }

    /**
     * 查询系统参数
     *
     * @param queryBean
     * @return
     * @throws Exception
     */
    public List<SysStaticData> querySysStaticData(SysStaticData queryBean, PageFilter pf) throws Exception {
        HashMap params = new HashMap();
        String hql = "from SysStaticData where 1 = 1";
        return staticDataDao.find(hql + sysStaticDataWhereHql(queryBean, params) + orderHql(pf), params, pf.getPage(), pf.getRows());
    }

    /**
     * 根据编号获取系统参数
     *
     * @param codeId
     * @return
     */
    public SysStaticData queryStaticDataByCodeId(long codeId) {
        SysStaticData sysStaticDataFDB = staticDataDao.get(SysStaticData.class, codeId);
        return sysStaticDataFDB;
    }

    /**
     * 系统参数拼装组装条件
     *
     * @param bean
     * @param params
     * @return
     */
    private String sysStaticDataWhereHql(SysStaticData bean, Map<String, Object> params) {
        String hql = "";
        if (bean != null) {
            if (StringUtils.isNotBlank(bean.getCodeKey())) {
                hql += " and codeKey like :codeKey";
                params.put("codeKey", "%" + bean.getCodeKey() + "%");
            }
            if (StringUtils.isNotBlank(bean.getCodeValue())) {
                hql += " and codeValue like :codeValue";
                params.put("codeValue", "%" + bean.getCodeValue() + "%");
            }
            if (StringUtils.isNotBlank(bean.getCodeName())) {
                hql += " and codeName like :codeName";
                params.put("codeName", "%" + bean.getCodeName() + "%");
            }
        }
        return hql;
    }
}
